package com.tj.shopping.service.impl;

import com.tj.shopping.entity.Carts;
import com.tj.shopping.entity.Goods;
import com.tj.shopping.enums.ResultEnum;
import com.tj.shopping.mapper.ShoppingCartMapper;
import com.tj.shopping.service.ShoppingCartService;
import com.tj.shopping.util.MybatisUtil;
import com.tj.shopping.vo.ResultVO;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: H
 * @create: 2023-12-20 15:28
 */
public class ShoppingCartServiceImpl implements ShoppingCartService {
        private ShoppingCartMapper mapper;
    @Override
    public Map<String,Object>finds(Map<String,Object>map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(ShoppingCartMapper.class);
        List<Map<String,Object>>goods=mapper.finds(map);
        System.out.println("this goods");
        System.out.println(goods);
        System.out.println(goods.size());
        Map<String,Object>map1=new HashMap<>();
        int total=mapper.total(map);
        System.out.println(total+"total");
        map1.put("count",total);

        if(goods==null||goods.isEmpty()){


            map1.put("code",600);
            map1.put("msg","暂无数据");


        }else {
            map1.put("code",200);
            map1.put("msg","成功");


        }
        map1.put("data",goods);
        MybatisUtil.close(session);
        return map1;
    }

    @Override
    public int Remove(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(ShoppingCartMapper.class);
        Map<String, Object> good=mapper.findOne(map).get(0);
        int uno=Integer.parseInt((String)map.get("uno"));
        System.out.println(uno);
        System.out.println(111);
        int result=mapper.Remove(uno,(int)good.get("gdno"));
        MybatisUtil.close(session);
        return  result;
    }

    @Override
    public int RemoveByCno(int cno) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(ShoppingCartMapper.class);
        int result=mapper.RemoveByCno(cno);
        MybatisUtil.close(session);
        return  result;
    }

    @Override
    public int deleteAll(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(ShoppingCartMapper.class);
        int result=mapper.deleteAll(map);
        MybatisUtil.close(session);
        return  result;

    }

    @Override
    public int addNum(Carts carts,Map<String,Object>map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(ShoppingCartMapper.class);

        Map<String,Object> good=mapper.findOne(map).get(0);
        carts.setGdno((int)good.get("gdno"));
        int result =mapper.addNum(carts);
        MybatisUtil.close(session);
        return  result;
    }


    @Override
    public Map<String, Object> carttotal(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(ShoppingCartMapper.class);
        int carttotal=mapper.carttotal(map);
        Map<String,Object> m=new HashMap<>();
        if (carttotal==0){
            m.put("code",600);
            m.put("msg","暂无数据");
        }else{
            m.put("code",200);
            m.put("msg","查询成功");
            m.put("carttotal",carttotal);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public Carts selectCart(int cno) {
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(ShoppingCartMapper.class);
        Carts carts = mapper.selectCart(cno);
        return carts;
    }


    @Override
    public Map<String, Object> addshoppingcart(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(ShoppingCartMapper.class);
        boolean addcart=mapper.addshoppingcart(map);
        int carttotal=mapper.carttotal(map);
        Map<String,Object> m=new HashMap<>();
        if (true==addcart){
            m.put("code",200);
            m.put("msg","添加成功");
            m.put("carttotal",carttotal);
        }else{
            m.put("code",600);
            m.put("msg","添加失败");
        }
        MybatisUtil.close(session);
        return m;
    }

}
